/**
 * 
 */
package fr.univ_tln.inf9.exaplanning.api.salle;

import java.io.IOException;
import java.sql.SQLException;
import java.util.LinkedList;
import java.util.List;

import javax.swing.event.EventListenerList;

import fr.univ_tln.inf9.exaplanning.api.db.AbstractEntity;
import fr.univ_tln.inf9.exaplanning.api.db.EntityInfo;
import fr.univ_tln.inf9.exaplanning.api.db.SessionManager;
import fr.univ_tln.inf9.exaplanning.api.salle.event.ListMaterialChangedEvent;
import fr.univ_tln.inf9.exaplanning.api.salle.event.ListMaterialListener;
import fr.univ_tln.inf9.exaplanning.api.salle.event.MaterialAddedToListEvent;
import fr.univ_tln.inf9.exaplanning.api.salle.event.MaterialRemovedToListEvent;
import fr.univ_tln.inf9.exaplanning.api.salle.event.TypeMaterialListener;

/**
 * @author pivi
 * @author emilien (persistance)
 */
@EntityInfo(	collections = { Material.class },
				key = "id",
				methods = { "getId", "getBuilding" },
				rows = { "id_room", "name" },
				table = "room",
				variables = { "building" }) 
public class Room extends AbstractEntity {
	
	private final String id;
	private final String building;
	private List<String> equipment = new LinkedList<String>();
	
	private EventListenerList listeners;
	
	/**
	 * @param id => Le numéro de salle
	 * @param equipement => la liste peut-être vide lors de l'appel au constructeur
	 * @throws SQLException 
	 * @throws IOException 
	 */
	public Room(String id, Building building) throws IOException, SQLException {
		super(true);
		this.id = id;
		this.building = building.getName();
		
		listeners = new EventListenerList();
	}
	
	private Room(String id, String building, List<String> equipement) throws IOException, SQLException {
		super(false);
		this.id = id;
		this.building = building;
		this.equipment = equipement;
	}
	
	/**
	 * @return the id
	 */
	public String getId() {
		return id;
	}
	/**
	 * @return the equipmnt
	 */
	public List<String> getEquipment() {
		return equipment;
	}
	/**
	 * @param equipement the equipment to set
	 * fireListEquipementChanged() alerte que la liste d'équipements a changé
	 */
	public void setEquipment(List<String> equipment) {
		this.equipment = equipment;
		fireListEquipementChanged();
	}
	/**
	 * @param arg0
	 * @return
	 * @see java.util.List#add(java.lang.Object)
	 * 
	 * fireAddMaterialToList(material) alerte qu'un matériel a été rajouté à la liste
	 */
	public boolean add(Material arg0) {
		
		boolean res = equipment.add(arg0.getType());
		fireAddMaterialToList(arg0.getType());
		return res;
	}
	

	/**
	 * @param arg0
	 * @return
	 * @see java.util.List#remove(java.lang.Object)
	 * 
	 * fireRemoveMaterialToList() alerte qu'un materiel a été supprimé de la liste 
	 */
	public boolean remove(String arg0) {
		boolean result = equipment.remove(arg0);
		fireRemoveMaterialToList(arg0);
		
		return result;
	}

	
	

	public String EquipmentToString() {
		return "Room [equipment=" + equipment + "]";
	}
	
	public static Room findByKey(String key) throws IOException, SQLException {
	    	List<List<String>> data = SessionManager.getInstance().getData(Room.class, key);
	    	
	    	List<String> variables = data.get(0);
	    	Room result = new Room(variables.get(0), variables.get(1), data.get(1));
	    	
	    	return result;
	}
	
	/****************************************************************************************************************/
	/****************************************			LISTENER				*************************************/
	/****************************************************************************************************************/

	public void addListMaterialListener(ListMaterialListener listener){
		listeners.add(ListMaterialListener.class, listener);
	}
	
	public void removeListMaterialListener(ListMaterialListener l){
		 listeners.remove(ListMaterialListener.class, l);
	}
	
	
	/**
	 * Methode pour avertir qu'un changement de liste à eu lieu
	 */
	private void fireListEquipementChanged() {
		ListMaterialListener[] listenerList = (ListMaterialListener[])listeners.getListeners(ListMaterialListener.class);
		
		for(ListMaterialListener listener : listenerList){
			listener.ListMaterialChanged(new ListMaterialChangedEvent(this, getEquipment()));
		}
		
	}
	
	
	private void fireAddMaterialToList(String elementAdded) {
		ListMaterialListener[] listenerList = (ListMaterialListener[])listeners.getListeners(ListMaterialListener.class);
		
		for(ListMaterialListener listener : listenerList){
			listener.MaterialAddedToList(new MaterialAddedToListEvent(this, elementAdded));
		}
		
	}
	
	
private void fireRemoveMaterialToList(String elementRemoved) {
		ListMaterialListener[] listenerList = (ListMaterialListener[])listeners.getListeners(ListMaterialListener.class);
		
		for(ListMaterialListener listener : listenerList){
			listener.MaterialRemovedToList(new MaterialRemovedToListEvent(this, elementRemoved));
		}
		
	}

public String getBuilding() {
	return building;
}
	
	
	
	
}
